#import "SMRSubtleResizeControl.h"
#import "SMRSubtleScrollView.h"


#pragma mark Constants
static const CGFloat SMRSubtleResizeControlBorderPadding = 2.0f;
static const CGFloat SMRSubtleResizeControlLineSpacing = 2.0f;


#pragma mark -
#pragma mark Class Globals
static NSColor *SMRSubtleResizeControlHandleColor;
static NSColor *SMRSubtleResizeControlHighlightedHandleColor;
static NSColor *SMRSubtleResizeControlHandleColorSelected;
static NSColor *SMRSubtleResizeControlHighlightedHandleColorSelected;


#pragma mark -
@interface SMRSubtleResizeControl (SMRSubtleResizeControlPrivateMethods)
-(NSTrackingArea *)trackingArea_;
@end


#pragma mark -
@implementation SMRSubtleResizeControl

@synthesize parentScrollView;
@synthesize selected;


#pragma mark -
#pragma mark Initialization

+(void)initialize {
	SMRSubtleResizeControlHandleColor = [[NSColor colorWithCalibratedWhite:0.6 alpha:1.0f] retain];
	SMRSubtleResizeControlHighlightedHandleColor = [[NSColor colorWithCalibratedWhite:0.95f alpha:1.0f] retain];
	SMRSubtleResizeControlHandleColorSelected = [[NSColor colorWithCalibratedRed:0.337 green:0.384 blue:0.451 alpha:1.000] retain];
	SMRSubtleResizeControlHighlightedHandleColorSelected = [[NSColor colorWithCalibratedWhite:0.9f alpha:0.3f] retain];
}


#pragma mark -
#pragma mark Drawing

-(void)drawRect:(NSRect)dirtyRect {
	NSRect bounds = [self bounds];
	CGFloat maxx = NSMaxX(bounds) - SMRSubtleResizeControlBorderPadding + 0.5;
	CGFloat miny = NSMinY(bounds) + SMRSubtleResizeControlBorderPadding + 0.5;
	CGFloat currentLength = 1;
	NSRect cornerPointRect = NSMakeRect(NSMaxX(bounds) - SMRSubtleResizeControlBorderPadding, NSMinY(bounds) + SMRSubtleResizeControlBorderPadding, 1, 1);
	NSColor *normalColor = [self selected] ? SMRSubtleResizeControlHandleColorSelected : SMRSubtleResizeControlHandleColor;
	NSColor *highlightedColor = [self selected] ? SMRSubtleResizeControlHighlightedHandleColorSelected : SMRSubtleResizeControlHighlightedHandleColor;
	[normalColor setFill];
	[NSBezierPath fillRect:cornerPointRect];
	BOOL drawingHighlight = YES;
	[[NSGraphicsContext currentContext] setShouldAntialias:NO];
	for (NSUInteger lineIndex = 0; lineIndex < 5; lineIndex++) {
		NSColor *handleColor = drawingHighlight ? highlightedColor : normalColor;
		[handleColor setStroke];
		NSPoint bottomLeft = NSMakePoint(maxx - currentLength, miny);
		NSPoint topRight = NSMakePoint(maxx, miny + currentLength);
		[NSBezierPath strokeLineFromPoint:bottomLeft toPoint:topRight];
		currentLength += drawingHighlight ? SMRSubtleResizeControlLineSpacing : 1;
		drawingHighlight = !drawingHighlight;
	}
}


#pragma mark -
#pragma mark Event handling

//-(void)updateTrackingAreas {
//	for (NSTrackingArea *trackingArea in [self trackingAreas])
//		[self removeTrackingArea:trackingArea];
//	[self addTrackingArea:[self trackingArea_]];
//}
//
//-(NSTrackingArea *)trackingArea_ {
//	return [[[NSTrackingArea alloc] initWithRect:[self bounds] options:NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow owner:self userInfo:nil] autorelease];
//}

-(void)mouseDown:(NSEvent *)theEvent {
	CGFloat maxHeight = FLT_MAX;
	CGFloat minHeight = 0;
	id <SMRSubtleScrollViewDelegate> scrollViewDelegate = [[self parentScrollView] delegate];
	if ([scrollViewDelegate respondsToSelector:@selector(maximumResizeHeightOfScrollView:)])
		maxHeight = [scrollViewDelegate maximumResizeHeightOfScrollView:[self parentScrollView]];
	if ([scrollViewDelegate respondsToSelector:@selector(minimumResizeHeightOfScrollView:)])
		minHeight = [scrollViewDelegate minimumResizeHeightOfScrollView:[self parentScrollView]];
	NSView *resizeView = [self parentScrollView];
	BOOL usingSubstituteView = [scrollViewDelegate respondsToSelector:@selector(substituteResizeViewForScrollView:)];
	if (usingSubstituteView)
		resizeView = [scrollViewDelegate substituteResizeViewForScrollView:[self parentScrollView]];
	CGFloat mouseDownY = [theEvent locationInWindow].y;
	NSRect parentRect = [resizeView frame];
	BOOL useFlippedCoordinates = [[resizeView superview] isFlipped];
	while ([theEvent type] != NSLeftMouseUp) {
		CGFloat mouseY = [theEvent locationInWindow].y;
		CGFloat delta = mouseDownY - mouseY;
		NSRect resizedParentRect = parentRect;
		CGFloat newHeight = resizedParentRect.size.height + delta;
		newHeight = fmin(newHeight, maxHeight);
		newHeight = fmax(newHeight, minHeight);
		if (!useFlippedCoordinates) 
			resizedParentRect.origin.y -= newHeight - resizedParentRect.size.height;
		resizedParentRect.size.height = newHeight;
		if ([resizeView frame].size.height != resizedParentRect.size.height) {
			if ([resizeView respondsToSelector:@selector(resizeWithRect:)])
				[resizeView performSelector:@selector(resizeWithRect:) withObject:[NSValue valueWithRect:resizedParentRect]];
			else if ([scrollViewDelegate respondsToSelector:@selector(scrollView:resizeView:withRect:)])
				[scrollViewDelegate scrollView:[self parentScrollView] resizeView:resizeView withRect:resizedParentRect];
		}
		
//			[resizeView setFrame:resizedParentRect];
		theEvent = [[self window] nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask)];
	}
}

//-(void)mouseEntered:(NSEvent *)theEvent {
//	[[self parentScrollView] setResizeControlActive:YES];
//}
//
//-(void)mouseExited:(NSEvent *)theEvent {
//	[[self parentScrollView] setResizeControlActive:NO];
//}

@end
